<template>
	<div class="table-body-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-role-search mb15">
				<el-input size="default" v-model="state.tableData.param.label" placeholder="请输入角色名称" style="max-width: 180px"> </el-input>
				<el-button size="default" @click="getTableData" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAdd(state.add)">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增角色
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="name" label="名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="code" label="权限字符" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEdit(state.edit, scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination
                v-show="state.tableData.total>0"
                :total="state.tableData.total"
                v-model:page="state.tableData.param.current"
                v-model:limit="state.tableData.param.size"
                @pagination="getTableData"
            />
		</el-card>
		<RoleDialog ref="roleDialogRef" :menu-data="state.menuData" :dept-data="state.deptData" :scope-data="data_scope" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemRole">
import { roleApi } from '/@/api/system/role';
import { menuApi } from '/@/api/system/menu';
import { deptApi } from '/@/api/system/dept';
import constConfig from '/@/utils/constConfig';

// 引入组件
const RoleDialog = defineAsyncComponent(() => import('/@/views/system/role/dialog.vue'));

// 定义变量内容
const roleDialogRef = ref();
const { proxy } = <any>getCurrentInstance();
const { data_scope } = proxy.useDict('data_scope');
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			label:'',
			...constConfig.pageModel,
		},
	},
	deptData: [],
	menuData: [],
	formData: {
		name: '', 
		code: '', 
		status: '0', 
		dataScope:'',
		resIds:[],
        deptIds:[]
	},
	...constConfig.dialogType
});

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	roleApi().page(state.tableData.param).then(res=>{
		state.tableData.data = res.data.records??[];
		state.tableData.total = res.data.total;
		state.tableData.loading = false;
	})
};
// 打开新增角色弹窗
const onOpenAdd = (type: string) => {
	let row = JSON.parse(JSON.stringify(state.formData));
	roleDialogRef.value.openDialog(type, row);
};
// 打开修改角色弹窗
const onOpenEdit = (type: string, row: RowRoleType) => {
	row = JSON.parse(JSON.stringify(row))
	roleDialogRef.value.openDialog(type, row);
};
// 删除角色
const onRowDel = (row: RowRoleType) => {
	ElMessageBox.confirm(`${constConfig.delString.tip1 + row.name + constConfig.delString.tip2}`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
			roleApi().del(row.id).then(() => {
				getTableData();
			})
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	getTableData();
	deptApi().tree().then((res: any) => {
		state.deptData = res.data
	})
	menuApi().getMenuListTree().then((res: any) => {
		state.menuData = res.data
	})
});
</script>
<style scoped lang="scss">
.table-body-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>